/**
 * 
 */
package cw.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cw.entity.ArrangeGroup;
import cw.entity.Group;
import cw.entity.Team;
import cw.entity.User;
import cw.page.PageUtil;
import cw.service.ArrangeGroupService;
import cw.service.GroupService;
import cw.service.TeamService;
import cw.service.UserService;
import cw.serviceImp.UserServiceImpl;
import cw.util.DES;
import cw.util.Log;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("arrangeGroup")
public class ArrangeGroupController {
	
	private static Logger logger = Logger.getLogger(ArrangeGroupController.class);

	@Resource
	private GroupService groupServiceImpl;
	
	@Resource
	private ArrangeGroupService arrangeGroupServiceImpl;
	
	@Resource
	private TeamService teamServiceImpl;
	
	
	/**
     * 默认页面
     */
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.GET})
    public String list(HttpServletRequest request){
        return "showArrangeGroup";
    }
	
	@RequestMapping(value="json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> showArrangeGroup(HttpServletRequest request) {
		/*
		private int pageSize;//每页显示的条数 
	    private int recordCount;//总共的条数 
	    private int currentPage;//当前页面 
	    //总页数 int getPageCount()     
        //包含，起始索引为0 getFromIndex()
        //不包含 getToIndex()
	    */
		
	    
      String groupId = request.getParameter("groupId");
      String description =request.getParameter("description");
      String id = request.getParameter("id");
      
      Integer total = arrangeGroupServiceImpl.getTotal(groupId, description, id);
      System.out.print("---"+total);
      
      Integer pageSize = Integer.parseInt(request.getParameter("rows"));
      
      
      Integer page = Integer.parseInt(request.getParameter("page"));
      
      PageUtil pageUtil = new PageUtil(pageSize,total,page);
      
      List<ArrangeGroup> arrangeGroups = arrangeGroupServiceImpl.queryArrangeGroup(groupId, description, id, pageUtil.getFromIndex(), pageUtil.getToIndex());
      
      Map<String,Object> map = new HashMap<>();
      
      map.put("total", total);
      map.put("rows", arrangeGroups);
       
      return map;
   }
	
	/**
     * 创建
     */
    @RequestMapping(value = "create", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String createForm(HttpServletRequest request) {
    	
    	//得到组数
    	int num = Integer.parseInt(request.getParameter("num"));
    	
    	String result = arrangeGroupServiceImpl.arrangeGroup(num);
        
        return result;
    }
    
    

    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(HttpServletRequest request) {
    	int num = arrangeGroupServiceImpl.deleteByPrimaryKey();
    	int num1 = groupServiceImpl.deleteAll();
    	Log.write("删除成功!"+new Date().toString());
        return "success";
    }
	
}
